<?php
/**
 * 商品限时抢购
 * User: yuanxuncheng
 * Date: 2016-12-21
 * Time: 22:39
 */

namespace app\api\model\dao;

use think\Model;
use think\Db;

class FlashSale extends Model
{
    //set talbename
    protected $table = 'flash_sale';
    //set primary key
    protected $pk = 'id';

    /**
     * @desc 添加限时抢购
     * @param $param
     * @return false|int
     */
    public function addFlashSale($param)
    {
        return $this->allowField(true)->save($param);
    }

    /**
     * @desc 根据条件获取秒杀商品
     * @param $conds $fields
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function geFlashSaleByCond($conds = [], $fields = [], $offset = 0, $limit = 10000, $orderby = '')
    {
        $this->allowField(true);

        $arrCond = [];
        foreach ($conds as $key => $val) {
            //    if (in_array($key, $this->field)) {
            $arrCond[$key] = $val;
            //    }
        }
        empty($arrCond) && $arrCond = '1=1';

        $arrField = [];
        foreach ($fields as $val) {
            //    if (in_array($val, $this->field)) {
            $arrField[] = $val;
            //    }
        }
        empty($arrField) && $arrField = $this->field;

        return Db::table($this->table)->where($arrCond)->limit($offset, $limit)->order($orderby)->field($arrField)->select();
    }

    public function getCount($conds = [], $field = 'goods_id')
    {
        $this->allowField(true);

        $arrCond = [];
        foreach ($conds as $key => $val) {
            if (in_array($key, $this->field)) {
                $arrCond[$key] = $val;
            }
        }
        empty($arrCond) && $arrCond = '1=1';
        return Db::table($this->table)->where($arrCond)->count($field);
    }

    /**
     * @desc 获取限时抢购
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getFlashSale()
    {
        $field = '*';
        return Db::table($this->table)->field($field)->select();
    }

    /**
     * @return \think\paginator\Collection
     */
    public function getFlashSalePage()
    {
        return $this->paginate(10);
    }

    /**
     * @param $id
     * @return int
     * @throws \think\Exception
     */
    public function deleteFlashSale($id)
    {
        $conds = [
            'id' => $id
        ];
        return $this->where($conds)->delete();
    }

    /**
     * @param $param
     * @return false|int
     */
    public function editFlashSale($param)
    {
        $conds = array(
            'id' => $param['id'],
        );
        $field = [];
        foreach ($param as $key => $value) {
            if ($key == 'id') {
                continue;
            }
            $field[$key] = $value;
        }
        return $this->isUpdate(true)->allowField(true)->save($field, $conds);
    }

    public function getStartTimeAttr($value)
    {
        return date("Y-m-d H:i:m", $value);
    }

    public function getEndTimeAttr($value)
    {
        return date("Y-m-d H:i:m", $value);
    }
}
